/*
 * @author java_deng
 * @date 2025/2/5 11:49
 * @description
 */
package org.hbwl.controller;

import cn.hutool.json.JSONUtil;
import org.hbwl.dto.User;
import org.hbwl.po.UserPo;
import org.hbwl.service.UserService;
import org.hbwl.utils.JwtUtil;
import org.hbwl.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.management.ServiceNotFoundException;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController{
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Object login(@RequestBody User userPo){
        String exception=checkParams(userPo);
        if(exception!=null){
            return fail(exception);
        }
        UserVo user=userService.login(userPo);
        if(user!=null){
            Map<String,Object> info = new HashMap<>();
            info.put("username",user.getUsername());
            info.put("account",user.getAccount());
            String token = JwtUtil.createJWT(UUID.randomUUID().toString(), JSONUtil.toJsonStr(info), null);
            user.setToken(token);
            return success(user);
        }
        else {
            return fail("登录失败，请检查账号或密码");
        }
    }

    @PostMapping("/register")
    public Object register(@RequestBody User userDto){
        try {
            checkRegisterParams(userDto);
            if(userDto.getId()!=null){
                userService.updateUserInfo(userDto);
            }else {
                userService.save(userDto);
            }
        }catch (ServiceNotFoundException exception){
            return fail(exception.getMessage());
        }
        return success("注册成功");
    }

    @GetMapping("/updateProfile")
    public Object updateProfile(User userDto){
        Long profile = userDto.getProfile();
        Long id = userDto.getId();
        try {
            if(profile==null){
                throw new ServiceNotFoundException("未检测到头像");
            }
            if(id==null){
                throw new ServiceNotFoundException("未检测到id");
            }
            UserVo update = userService.updateUserInfo(userDto);

            return success("修改成功",update);
        }catch (ServiceNotFoundException e){
            return fail(e.getMessage());
        }
    }

    @PostMapping("/updateInfo")
    public Object updateUserInfo(@RequestBody User userDto){
        Long id = userDto.getId();
        if(id==null){
            return fail("id不能为空");
        }
        UserVo update = userService.updateUserInfo(userDto);
        return success("修改成功",update);
    }

    @GetMapping("/getSystemInfo")
    public Object getSystemInfo(){
        return success(userService.getSystemInfo());
    }

    @PostMapping("/managerList")
    public Object managerList(@RequestBody  UserPo userPo){
        return success(userService.managerList(userPo));
    }

    @GetMapping("/delete")
    public Object delete(Long id){
        userService.delete(id);
        return success("删除成功");
    }

    private void checkRegisterParams(User userDto) throws ServiceNotFoundException {
        String name = userDto.getUsername();
        String email = userDto.getEmail();
        String account = userDto.getAccount();
        String password = userDto.getPassword();
        String gender = userDto.getSex();

        System.out.println(userDto);
        if(account==null||account.isEmpty()){
            throw new ServiceNotFoundException("account不能为空");
        }
        if(name==null||name.isEmpty()){
            throw new ServiceNotFoundException("name不能为空");
        }
        if(password.length()<8){
            throw new ServiceNotFoundException("密码不能少于8位数");
        }
        if(email==null||email.isEmpty()){
            throw new ServiceNotFoundException("email不能为空");
        }
        if(password==null||password.isEmpty()){
            throw new ServiceNotFoundException("password不能为空");
        }
    }

    private String checkParams(User userDto) {
        String password = userDto.getPassword();
        String account = userDto.getAccount();
        if(password==null){
            return "密码不能为空";
        }
        if(account==null){
            return "账号不能为空";
        }
        return null;
    }
}
